<?php

include_once 'DataProvider.php';

class User {
    public $dataProvider;

    public function __construct() {
        $this->dataProvider = new dataProvider();
    }

    public function kiemtradangnhap($user, $pass) {
        $sql = "select * from `users` as u where u.UserName = '$user' and u.Pass = '$pass' and u.Loai_User != 1";
        $row = $this->dataProvider->GetOneRow($sql);
        if (!empty($row)) {
            $_SESSION['session_id_user'] = $row['UserName'];
            $_SESSION['session_id_email'] = $row['Email'];
            return true;
        }
        return false;
    }

    public function layDanhSachThanhVien($_start, $_display) {
        $sql = "select * from `users` WHERE Loai_User = 2 limit $_start, $_display";
        $tmp = $this->dataProvider->ExecuteQuery($sql);
        $result = array();
        while ($row = mysql_fetch_assoc($tmp)) {
            $result[] = $row;
        }
        return $result;
    }
    
    public function DemThanhVien() {
        $sql = "select count(idUsers) from `users` WHERE Loai_User = 2";
        return DataProvider::GetOneRowi($sql);
    }

    public function xoaThanhVien($_idUsers) {
        $sql = "DELETE FROM `users` WHERE idUsers = '". $_idUsers . "'";
        return DataProvider::ExecuteQuery($sql);
    }
    
    public function layThanhVien($idUsers) {
        $sql = "SELECT * from `users` WHERE `users`.idUsers = '$idUsers'";
        return DataProvider::GetOneRow($sql);
    }
    public function layKhachHang($UserName){
        $sql = "select * from `users` WHERE `users`.UserName ='$UserName'";
        return DataProvider::GetOneRow($sql);
    }
    public function capnhapThanhVien($idUsers,$data){
        $sql = "UPDATE `users` 
                SET `users`.FirstName = '{$data['FirstName']}',
                    `users`.LastName = '{$data['LastName']}',
                    `users`.Email= '{$data['Email']}', 
                    `users`.Birthday = '{$data['Birthday']}', 
                    `users`.Location = '{$data['Location']}',
                    `users`.PhoneNum = '{$data['PhoneNum']}' 
                where `users`.idUsers = '$idUsers'";                   
        $result = $this->dataProvider->ExecuteQuery($sql);
        return $result;
    }
    public function capnhapTaiKhoan($UserName,$data){
        $sql = "UPDATE `users` 
                SET `users`.FirstName = '{$data['FirstName']}',
                    `users`.LastName = '{$data['LastName']}',
                    `users`.Location = '{$data['Location']}',
                    `users`.PhoneNum = '{$data['PhoneNum']}' 
                where `users`.UserName = '$UserName'";                   
        $result = $this->dataProvider->ExecuteQuery($sql);
        return $result;
    }
    public function timThanhVien($UserName) {
        $sql = "SELECT * from `users` WHERE `users`.UserName like '%$UserName%' and Loai_User=2";
        return DataProvider::ExecuteQuery($sql);
    }
}
?>
